import { gameConfigHandle } from "core/config/gameConfig"
import Random from "core/utils/random"
import utils from "core/utils/utils"
import { ItemModel, SkillItemModel } from "core/model/itemModel"
import itemHandle from "./itemHandle"
import { PlayerModel } from "core/model/playerModel"
import bagHandle from "./bagHandle"
import { FighterSkillModel } from "core/model/skillModel"
import { PartnerModel } from "core/model/partnerModel"

class SkillHandle {
    getFriendshipSkillOfHero(player: PlayerModel, heroId: number) {
        let friendship = player.friendship
        let disciples = Object.entries(friendship.disciples)
        let friendshipId: number = null
        for (let i = 0; i < disciples.length; i++) {
            let id = disciples[i][1] as number
            if (id == heroId) {
                friendshipId = Number(disciples[i][0])
                break
            }
        }
        if (friendshipId) {
            let index = friendship.friendshipSkills[friendshipId] || 0
            let skillId = gameConfigHandle.configByID[friendshipId].skills[index]
            return skillId
        }
        return null
    }
    createBook(random: Random, { skillId, lucky, minQuality, maxQuality, quality, lSkills }: any) {
        let skillConfig = gameConfigHandle.gameConfig.skill
        let itemID = skillConfig.skillItemTID
        if (utils.isNull(skillId)) {
            skillId = utils.getOneFromArray(skillConfig.skillList, random)
        }
        let skillBookItem = new SkillItemModel()
        skillBookItem.tid = skillId
        let item = itemHandle.create({ tid: itemID })

        if (utils.isNull(quality)) {
            quality = utils.getQualityByodds(lucky || 0, minQuality || 0, maxQuality || 3, skillConfig.qualityOdds, random)
        }


        if (!utils.isNull(lSkills)) {
            skillBookItem.aSkillIDs = lSkills
        } else {
            let aSkillIDList = utils.getRandomArrayNoRepeat(skillConfig.lSkill, quality, random)
            let aSkillIDs = []
            let totalLvl = 0
            aSkillIDList.forEach(lskillID => {
                let skillLvl = utils.getRandomNumber(1, 10, random)
                if (lskillID !== 270109) {
                    totalLvl += skillLvl
                }
                aSkillIDs.push({ skillID: lskillID, lvl: skillLvl })
            })
            skillBookItem.aSkillIDs = aSkillIDs
        }
        let totalLvl = 0
        skillBookItem.aSkillIDs.forEach(item => {
            if (item.skillID !== 270109) {
                totalLvl += item.lvl
            }
        })
        let needMp = utils.getIncValues(skillConfig.needMp, totalLvl)
        skillBookItem.quality = quality
        skillBookItem.mp = needMp
        item.skill = skillBookItem
        return item
    }

    destroy(player: PlayerModel, item: ItemModel) {
        let skillConfig = gameConfigHandle.gameConfig.skill
        let getItem = {}
        skillConfig.destroy[item.skill.tid].forEach(e => {
            let name: any = e.itemID
            let values = e.amount
            let value = values[0] + values[1] * item.skill.quality
            if (isNaN(name)) {
                player.itemInfo[name] += value
            } else {
                bagHandle.addItem(player.bag, Number(name), value)
            }
            getItem[name] = value
        })
        if (item.skill.quality >= 2) {
            skillConfig.destroy2.forEach(e => {
                let name: any = e.itemID
                let values = e.amount
                let value = values[0]
                if (isNaN(name)) {
                    player.itemInfo[name] += value
                } else {
                    bagHandle.addItem(player.bag, Number(name), value)
                }
                getItem[name] = value
            })
        }
        return getItem
    }

    calcSkill(partner: PartnerModel, skillItem: ItemModel) {
        let skill = new FighterSkillModel()
        skill.tid = skillItem.skill.tid
        skill.lvl = 1
        skill.quality = skillItem.skill.quality
        skill.mp = skillItem.skill.mp
        skill.aSkills = []
        skillItem.skill.aSkillIDs.forEach((aSkillItem) => {
            skill.aSkills.push([aSkillItem.skillID, aSkillItem.lvl2 || aSkillItem.lvl])
        })

        let skillID = skillItem.skill.tid
        let skillLvl = (partner.skillLvls[skillID] || 1)
        let skillConfig = gameConfigHandle.configByID[skillID]
        let starLvl = 1 + Math.floor(skillLvl / 10)
        skill.starLvl = starLvl

        skill.aSkills.forEach(item => {
            let askillID = item[0]
            let askillLvl = item[1] || 1
            let askillConfig = gameConfigHandle.configByID[askillID]

            if (askillConfig.skillAttris) {
                utils.entries(askillConfig.skillAttris).forEach(attri => {
                    let attriName = attri[0]
                    let values: any = attri[1]
                    skill[attriName] += utils.getIncValues(values, askillLvl)
                })
            }
        })

        if (skillConfig.cd) {
            skill.cd = skillConfig.cd - skill.cd

            if (skill.cdPer) {
                skill.cd = skill.cd * (100 - skill.cdPer) / 100
            }

        }
        skill.mp = this.calcSkillMp(skillItem)
        if (skill.reduceMpPer) {
            skill.mp = skill.mp * (100 - skill.reduceMpPer) / 100
        }
        return skill

    }

    calcSkillMp = (skillItem: ItemModel) => {
        let skillConfig = gameConfigHandle.gameConfig.skill
        let totalLvl = 0
        skillItem.skill.aSkillIDs.forEach(item => {
            if (item.skillID !== 270109) {
                if (item.lvl2) {
                    totalLvl += item.lvl2
                } else {
                    totalLvl += item.lvl
                }

            }
        })
        let needMp = utils.getIncValues(skillConfig.needMp, totalLvl)
        return needMp
    }
}

let skillHandle = new SkillHandle
export default skillHandle